﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RyanLam.NET.Core.ITemplates;
using RyanLam.NET.Core.Menu;
using WebComponents.Data;
using System.Data;
using System.Web;

namespace RyanLam.NET.Core.Controllers
{
    public static class GetMenuRoles
    {

        /// <summary>
        /// 
        /// </summary>
        /// <param name="menu"></param>
        /// <returns></returns>
        public static List<MenuRoles> GetMenuRolesByMenuID(this IMenu menu, string oncase)
        {
            List<MenuRoles> list = new List<MenuRoles>();
            List<GetData.ISqlParameter> paramUser = new List<GetData.ISqlParameter>();
            paramUser.Add(new GetData.ISqlParameter { name = "@MenuID", value = menu.ID.ToString() });
            DataTable dt = new DataTable();
            dt = GetData.DataTable("SELECT MenuID, RoleID, UserID FROM MenuRoles WHERE MenuID=@MenuID", paramUser);
            foreach (DataRow dr in dt.Rows)
            {
                list.Add(new MenuRoles
                {
                    MenuID = Convert.ToInt32(dr["MenuID"]),
                    RoleID = dr.IsNull("RoleID") ? String.Empty.ToGuid() : dr["RoleID"].ToString().ToGuid(),
                    UserID = dr.IsNull("UserID") ? String.Empty.ToGuid() : dr["UserID"].ToString().ToGuid()
                });
            }
            List<MenuRoles> newlist = new List<MenuRoles>();
            switch (oncase)
            {
                case "roles":
                    var queryRoles = from t in list
                                     where t.RoleID != null
                                     select t;
                    newlist = queryRoles.ToList();
                    break;
                case "users":
                    var queryMembers = from t in list
                                       where t.UserID != null
                                       select t;
                    newlist = queryMembers.ToList();
                    break;
                default:
                    break;
            }
            return newlist;
        }
    }
}
